
package com.scs.application.modules.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.BeanValidators;
import com.scs.application.modules.base.entity.MaterialApplicantItem;
import com.scs.application.modules.base.request.MaterialApplicantItemQueryRequest;
import com.scs.application.modules.base.service.MaterialApplicantItemService;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * MaterialApplicantItem controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/base/material_applicant_items")
public class MaterialApplicantItemController extends CrudController<MaterialApplicantItem, String, MaterialApplicantItemService> implements ImAndExAbleController<MaterialApplicantItemQueryRequest> {

    @GetMapping("/page")
    public PageResult page(MaterialApplicantItemQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(MaterialApplicantItemQueryRequest request) {
        List<MaterialApplicantItem> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    /**
     * 批量保存供货明细
     *
     * @param children
     * @return
     */
    @PutMapping("/batch_save")
    public CommonResult batchSave(@RequestBody @Valid List<MaterialApplicantItem> children) {
        Assert.notEmpty(children, "至少有一条供货明细");
        BeanValidators.validateListWithException(validator, children);
        boolean bSuccess = service.saveOrUpdateBatch(children);
        return bSuccess ? CommonResult.ok("保存成功") : CommonResult.error("保存失败");
    }
}
